﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using NewsMine.Storage;

namespace NewsDiscoveryEngine
{
    [Serializable]
    public class HtmlLinksDedupeAgainstPreviousVisitJob : BaseJob
    {
        public string Url { get; set; }
        public List<HtmlLink> HtmlLinks { get; set; }

        public override bool ExecuteJob()
        {
            if (string.IsNullOrEmpty(Url))
                throw new ApplicationException("FeedsourceUrl can't be null or empty for deduping");

            if (HtmlLinks == null)
            {
                return true;
            }

            var results = DedupeHtmlLinks(this.Url, this.HtmlLinks);

            ProcessOutput(results);
            return true;
        }

        public static List<HtmlLink> DedupeHtmlLinks(string sourceUrl, List<HtmlLink> htmlLinks)
        {
            //todo:while deduping the links, there could be two links with diffrent titles. Some time week title will carry the rich image. This needs to be considered and handled.

            List<HtmlLink> lastTimeResultedHtmlLinksAgainstUrl = (List<HtmlLink>)Defaults.HtmlLinkStore.Get(sourceUrl);

            if (lastTimeResultedHtmlLinksAgainstUrl == null)
                return htmlLinks;

            List<HtmlLink> resultedItemsAfterDedupe = GetItemsAfterDedupe(htmlLinks, lastTimeResultedHtmlLinksAgainstUrl);
            return resultedItemsAfterDedupe;
        }

        private static List<HtmlLink> GetItemsAfterDedupe(List<HtmlLink> newHtmlLinksList, List<HtmlLink> lastTimeResultedHtmlLinksAgainstUrl)
        {
            //var DuplicateLinks = (from newfdItem in newHtmlLinksList
            //              join oldFdItem in lastTimeResultedHtmlLinksAgainstUrl
            //              on newfdItem.Link equals oldFdItem.Link
            //              select new { Link = newfdItem.Link }).ToList();

            List<HtmlLink> resultedItems = new List<HtmlLink>();

            foreach (var newHtmlLink in newHtmlLinksList)
            {
                var duplicateItem = (from oldHtmlLink in lastTimeResultedHtmlLinksAgainstUrl
                                     where oldHtmlLink.Link == newHtmlLink.Link 
                                     select oldHtmlLink).FirstOrDefault();

                //if there is no duplicate found against link, lets do same deduple logic against the title field.
                if (duplicateItem == null)
                {
                    duplicateItem = (from oldHtmlLink in lastTimeResultedHtmlLinksAgainstUrl
                                     where (!string.IsNullOrEmpty(oldHtmlLink.Title) &&
                                     !string.IsNullOrEmpty(newHtmlLink.Title) &&
                                     oldHtmlLink.Title == newHtmlLink.Title)
                                     select oldHtmlLink).FirstOrDefault();
                }

                if (duplicateItem == null)
                {
                    resultedItems.Add(newHtmlLink);
                }
            }

            return resultedItems;
        }
   
    }
}
